package global

import (
	"encoding/json"
	"github.com/PuerkitoBio/goquery"
	"io/ioutil"
	"log"
	"news/utils"
	"strings"
)

var CategoryId = 0

// BjNews 采集新京报
func BjNews(url string) {
	go func() {
		var data = utils.HttpGet(url)
		doc, err := goquery.NewDocumentFromReader(data)
		if err != nil {
			log.Fatal(err)
		}
		var rs News
		var bj []News
		doc.Find(".pin_demo_out").Each(func(i int, s *goquery.Selection) {
			rs.Title = utils.ReplaceAndTrim(s.Find("a").Text(), 166)
			url, _ = s.Find("a").Attr("href")
			//栏目id
			switch url {
			case "https://www.bjnews.com.cn/news":
				CategoryId = 1
			}
			var Content = utils.HttpGet(url)
			doc, _ := goquery.NewDocumentFromReader(Content)
			rs.Content = doc.Find(".content-name").Text()
			rs.Url = url
			rs.CategoryId = CategoryId
			bj = append(bj, rs)
		})
		Content <- bj
	}()
}

// ThePaper 采集彭拜网
func ThePaper(url string) {
	go func() {
		var data = utils.HttpGet(url)
		doc, err := goquery.NewDocumentFromReader(data)
		if err != nil {
			log.Fatal(err)
		}
		var rs News
		var result []News
		doc.Find(".newsbox .news_li").Each(func(i int, s *goquery.Selection) {
			rs.Title = utils.ReplaceAndTrim(s.Find("a").Text(), 166)
			urls, _ := s.Find("a").Attr("href")
			//获取链接的内容
			rs.Url = url + urls
			var Content = utils.HttpGet(url + urls)
			doc, _ := goquery.NewDocumentFromReader(Content)
			rs.Content = doc.Find(".news_txt").Text()
			rs.CategoryId = 9
			result = append(result, rs)
		})
		Content <- result
	}()
}

// NewsChina 采集中国新闻
func NewsChina(url string) {
	go func() {
		var data = utils.HttpGet(url)
		doc, err := goquery.NewDocumentFromReader(data)
		if err != nil {
			log.Fatal(err)
		}
		var rs News
		var bj []News
		doc.Find(".item_title").Each(func(i int, s *goquery.Selection) {
			rs.Title = utils.ReplaceAndTrim(s.Find("a").Text(), 166)
			url, _ := s.Find("a").Attr("href")
			var Content = utils.HttpGet(url)
			doc, _ := goquery.NewDocumentFromReader(Content)
			rs.Content = doc.Find(".article_content").Text()
			rs.Url = url
			rs.CategoryId = 8
			bj = append(bj, rs)
		})
		Content <- bj
	}()
}

// QIanLong 采集千龙网
func QIanLong(url string) {
	go func() {
		var data = utils.HttpGet(url)
		doc, err := goquery.NewDocumentFromReader(data)
		if err != nil {
			log.Fatal(err)
		}
		var rs News
		var result []News
		doc.Find(".s_pc_rdjx_box").Each(func(i int, s *goquery.Selection) {
			rs.Title = utils.ReplaceAndTrim(s.Find("a").Text(), 166)
			url, _ := s.Find("a").Attr("href")
			//栏目id
			//获取链接的内容
			Content := utils.HttpGet(url)
			rs.Url = url
			rs.CategoryId = 1
			doc, _ := goquery.NewDocumentFromReader(Content)
			rs.Content = doc.Find(".article-content").Text()
			result = append(result, rs)
		})
		Content <- result
	}()
}

// SouTHcn 采集南方网
func SouTHcn(url string) {
	go func() {
		var data = utils.HttpGet(url)
		doc, err := goquery.NewDocumentFromReader(data)
		if err != nil {
			log.Fatal(err)
		}
		var rs News
		var result []News
		doc.Find(".m-lists .pw").Each(func(i int, s *goquery.Selection) {
			rs.Title = utils.ReplaceAndTrim(s.Find("a").Text(), 166)
			urls, _ := s.Find("a").Attr("href")
			//获取链接的内容
			var Content = utils.HttpGet(urls)
			doc, _ := goquery.NewDocumentFromReader(Content)
			rs.Content = doc.Find(".content").Text()
			rs.Url = url
			rs.CategoryId = 7
			result = append(result, rs)
		})
		Content <- result
	}()
}

type AutoGenerated struct {
	Data struct {
		List []struct {
			ID        string
			Title     string
			FocusDate string
			URL       string
			Image     string
			Image2    string
			Image3    string
			Brief     string
			ExtField  string
			Keywords  string
			Count     string
		}
		Total int
	}
}

// Cctv 采集央视网
func Cctv(url string) {
	go func() {
		var data = utils.HttpGet(url)
		str, _ := ioutil.ReadAll(data)
		var strData *AutoGenerated
		left := strings.TrimLeft(string(str), "news(")
		trimStrings := strings.TrimRight(left, ")")
		json.Unmarshal([]byte(trimStrings), &strData)
		var rs News
		var result []News
		for _, v := range strData.Data.List {
			var data = utils.HttpGet(v.URL)
			doc, err := goquery.NewDocumentFromReader(data)
			if err != nil {
				log.Fatal(err)
			}
			var text string = doc.Find(".text_area").Text()
			rs.Content = text
			rs.Url = url
			rs.CategoryId = 1
			rs.Title = utils.ReplaceAndTrim(v.Title, 166)
			result = append(result, rs)
		}

		Content <- result
	}()
}

// Sina 新浪网
func Sina(url string) {
	go func() {
		var data = utils.HttpGet(url)
		doc, _ := goquery.NewDocumentFromReader(data)
		var rs News
		var result []News
		doc.Find(".news-item").Each(func(i int, s *goquery.Selection) {
			url, _ := s.Find("h2 a").Attr("href")
			if len(url) > 0 {
				rs.Title = utils.ReplaceAndTrim(s.Find("h2 a").Text(), 166)
				rs.Url = url
				var Content = utils.HttpGet(url)
				doc, _ := goquery.NewDocumentFromReader(Content)
				rs.Content = doc.Find(".article").Text()
				rs.CategoryId = 1
				result = append(result, rs)
			}
		})
		Content <- result
	}()
}
